import {getBiliDanmakuApi, getHttpPath} from '../../js/my-utils'
import {_ANIME_PATH_} from '../../js/my-core'
import {showMessage} from '../../js/my-plugin'
import {publicAcgInit} from '../../js/my-public'

// ckplayer 视频类型格式
// const ckPlayerType = ['video/mp4']
const dPlayerType = ['normal', 'hls']

export const getByAv = (_this, val) => {
  _this.$axios({
    method: 'get',
    url: '/api/anime/play/eq/' + val
  })
    .then((res) => {
      if (res.data.code === 200) {
        publicAcgInit(_this, _this.anime, res.data.data.anime)
        console.log('av', res.data.data.anime)
        _this.playerInit.video = []
        let i = 0
        for (let item of res.data.data.list) {
          let it = {id: i, no: item.no, title: item.title, auth: item.auth, href: _ANIME_PATH_ + item.id, play: false}
          if (Number(val) === item.id) {
            it.play = true
            _this.playerInit.current = it.id
            _this.playerInit.thumbnails = item.thumbnails == null ? '' : getHttpPath(item.thumbnails)
            _this.playerInit.addition = [getBiliDanmakuApi(item.cid)]
          }
          _this.playerInit.video.push(it)
          i++
        }
        videoPublic(_this, res.data.data.anime.id)
        getSource(_this, val)
      }
    })
    .catch((error) => {
      console.log(error)
    })
}

export const getByAn = (_this, val) => {
  _this.$axios({
    method: 'get',
    url: '/api/anime/play/ss/' + val
  })
    .then((res) => {
      if (res.data.code === 200) {
        publicAcgInit(_this, _this.anime, res.data.data.anime)
        _this.playerInit.video = []
        let i = 0
        let play = 0
        for (let item of res.data.data.list) {
          let it = {id: i, no: item.no, title: item.title, auth: item.auth, href: _ANIME_PATH_ + item.id, play: false}
          if (i === 0) {
            play = item.id
            it.play = true
            _this.playerInit.current = it.id
            _this.playerInit.thumbnails = item.thumbnails == null ? '' : getHttpPath(item.thumbnails)
            _this.playerInit.addition = [getBiliDanmakuApi(item.cid)]
          }
          _this.playerInit.video.push(it)
          i++
        }
        videoPublic(_this, res.data.data.anime.id)
        getSource(_this, play)
      }
    })
    .catch((error) => {
      console.log(error)
    })
}

const videoPublic = (_this, val) => {
  _this.anime.id = val
  getComments(_this, val)
  getThumb(_this, val)
  getCollect(_this, val)
  getCoins(_this, val)
}

const getSource = (_this, val) => {
  const data = {
    jwt: _this.$store.state.authorization,
    id: val
  }
  _this.$axios({
    method: 'post',
    url: '/api/anime/play/get_source',
    data: _this.$qs.stringify(data)
  })
    .then((res) => {
      _this.playerInit.quality = []
      if (res.data.code === 200) {
        for (let item of res.data.data) {
          const arr = {name: item.tag, url: getHttpPath(item.href), type: dPlayerType[item.type]}
          _this.playerInit.quality.push(arr)
        }
        _this.$refs.player.init()
      } else if (res.data.code === 204) {
        // 更新token
        _this.authorization = res.data.msg
        window.localStorage.setItem('authorization', res.data.msg)
        _this.reload()
      } else {
        showMessage(res.data.msg, 0)
        _this.$refs.player.init()
      }
    })
    .catch((error) => {
      showMessage(error, 0)
    })
}

const getThumb = (_this, val) => {
  _this.$axios({
    method: 'get',
    url: '/api/anime/thumb?animeId=' + val,
    headers: {Authorization: _this.$store.state.authorization}
  })
    .then((res) => {
      if (res.data.code === 200) {
        _this.isThumb = res.data.data
      } else if (res.data.code === 204) {
        // 更新token
        _this.authorization = res.data.msg
        window.localStorage.setItem('authorization', res.data.msg)
        _this.reload()
      } else {
        showMessage(res.data.msg, 0)
      }
    })
    .catch((error) => {
      showMessage(error, 0)
    })
}

export const setThumb = (_this, val) => {
  console.log('setThumb', val)
  let data = {
    animeId: val
  }
  _this.$axios({
    method: 'post',
    url: '/api/anime/thumb',
    headers: {Authorization: _this.$store.state.authorization},
    data: _this.$qs.stringify(data)
  })
    .then((res) => {
      if (res.data.code === 200) {
        _this.isThumb = !_this.isThumb
        if (_this.isThumb) {
          showMessage('点赞成功', 1)
        } else {
          showMessage('取消点赞成功', 1)
        }
      } else if (res.data.code === 204) {
        // 更新token
        _this.authorization = res.data.msg
        window.localStorage.setItem('authorization', res.data.msg)
        _this.reload()
      } else {
        showMessage(res.data.msg, 0)
      }
    })
    .catch((error) => {
      showMessage(error, 0)
    })
}

const getCollect = (_this, val) => {
  _this.$axios({
    method: 'get',
    url: '/api/anime/collect?animeId=' + val,
    headers: {Authorization: _this.$store.state.authorization}
  })
    .then((res) => {
      if (res.data.code === 200) {
        _this.isCollected = res.data.data
      } else if (res.data.code === 204) {
        // 更新token
        _this.authorization = res.data.msg
        window.localStorage.setItem('authorization', res.data.msg)
        _this.reload()
      } else {
        showMessage(res.data.msg, 0)
      }
    })
    .catch((error) => {
      showMessage(error, 0)
    })
}

export const setCollect = (_this, val) => {
  let data = {
    animeId: val
  }
  _this.$axios({
    method: 'post',
    url: '/api/anime/collect',
    headers: {Authorization: _this.$store.state.authorization},
    data: _this.$qs.stringify(data)
  })
    .then((res) => {
      if (res.data.code === 200) {
        _this.isCollected = !_this.isCollected
        if (_this.isCollected) {
          showMessage('追番成功', 1)
        } else {
          showMessage('取消追番成功', 1)
        }
      } else if (res.data.code === 204) {
        // 更新token
        _this.authorization = res.data.msg
        window.localStorage.setItem('authorization', res.data.msg)
        _this.reload()
      } else {
        showMessage(res.data.msg, 0)
      }
    })
    .catch((error) => {
      showMessage(error, 0)
    })
}

const getCoins = (_this, val) => {
  _this.$axios({
    method: 'get',
    url: '/api/anime/coins/' + val,
    headers: {Authorization: _this.$store.state.authorization}
  })
    .then((res) => {
      if (res.data.code === 200) {
        _this.isCoins = res.data.data
      } else if (res.data.code === 204) {
        // 更新token
        _this.authorization = res.data.msg
        window.localStorage.setItem('authorization', res.data.msg)
        _this.reload()
      } else {
        showMessage(res.data.msg, 0)
      }
    })
    .catch((error) => {
      showMessage(error, 0)
    })
}

export const setCoins = (_this, val) => {
  console.log('setCoins', val)
  let data = {
    animeId: val
  }
  _this.$axios({
    method: 'post',
    url: '/api/anime/coins',
    headers: {Authorization: _this.$store.state.authorization},
    data: _this.$qs.stringify(data)
  })
    .then((res) => {
      if (res.data.code === 200) {
        _this.isCoins = !_this.isCollected
        showMessage(res.data.msg, 1)
      } else if (res.data.code === 204) {
        // 更新token
        _this.authorization = res.data.msg
        window.localStorage.setItem('authorization', res.data.msg)
        _this.reload()
      } else {
        showMessage(res.data.msg, 0)
      }
    })
    .catch((error) => {
      showMessage(error, 0)
    })
}

const getComments = (_this, val) => {
  _this.$axios({
    method: 'get',
    url: '/api/anime/comment/' + val
  })
    .then((res) => {
      if (res.data.code === 200) {
        _this.comments = []
        let i = 0
        for (let item of res.data.data) {
          let it = {id: i, content: item.content, sendTime: new Date(item.sendTime), nickname: item.nickname, avatar: getHttpPath(item.avatar)}
          _this.comments.push(it)
          i++
        }
      } else {
        showMessage(res.data.msg, 0)
      }
    })
    .catch((error) => {
      showMessage(error, 0)
    })
}

export const sendComment = (_this) => {
  const data = {
    animeId: _this.anime.id,
    content: _this.commentText
  }
  _this.$axios({
    method: 'post',
    url: '/api/anime/comment',
    data: _this.$qs.stringify(data),
    headers: {Authorization: _this.$store.state.authorization}
  })
    .then((res) => {
      if (res.data.code === 200) {
        getComments(_this, _this.anime.id)
        showMessage(res.data.msg, 1)
      } else if (res.data.code === 204) {
        // 更新token
        _this.authorization = res.data.msg
        window.localStorage.setItem('authorization', res.data.msg)
        _this.reload()
      } else {
        showMessage(res.data.msg, 0)
      }
    })
    .catch((error) => {
      showMessage(error, 0)
    })
}

export const setHistory = (_this, val) => {
  const data = {
    animeId: _this.anime.id,
    animeVideoId: _this.playerInit.current,
    currentTime: val
  }
  _this.$axios({
    method: 'post',
    url: '/api/anime/set_history',
    data: _this.$qs.stringify(data),
    headers: {Authorization: _this.$store.state.authorization}
  })
    .then((res) => {
      if (res.data.code === 200) {
        console.log('上传成功')
      }
    })
    .catch((error) => {
      showMessage(error, 0)
    })
}
